# Clear
rm(list = ls())

# Set WD
setwd("conflict_prediction_replication_pkg")

# Load Packages
source("config_code/directories.R")
source("config_code/package_requirements.R")
source("config_code/helper_functions.R")
source("config_code/model_parameters.R")

cl <- makeCluster(10)
registerDoParallel(cl)

countries <- c('indo', 'colo')
vars <- c('any', 'high', 'spike')

for (table in c('tableA1')) {
  print('No models need to be run ahead of the figure code. (As long as table1 has run)')
}
for (table in c('tableA2')) {
  for (country in countries) { # {indo, colo}
    for (v in vars) { # {any, high, spike}
      source(paste("config_code/",country,"_data_setup.R",sep = ""))
      source(paste("config_code/",country,"_dependent_vars.R",sep = ""))
      source(paste("config_code/",country,"_predictor_vars.R",sep = ""))
      
      for (rhs.group in names(get(table))) { 
        
        rhs <- get(table)[[rhs.group]]
        for (algo in c("lasso",
                       "gbm",
                       "rf",
                       "nn",
                       "ebma"
        )) {
          print(paste(v,rhs.group,algo,sep="---"))
          source(paste("estimation_code/appendix/min_mse/conflict_",algo,"_mse.R",sep = ""))
        } 
      }
    }
  }
}


for (table in c('tableA3_pt1',
                'tableA3_pt2',
                'tableA3_pt3',
                'tableA3_pt4',
                'tableA3_pt5',
                'figure_A1_fixed',
                'figure_A1_annual',
                'figure_A1_slow')) {
  for (country in countries) { # {indo, colo}
    for (v in c("count")) {
      source(paste("config_code/",country,"_data_setup.R",sep = ""))
      source(paste("config_code/",country,"_dependent_vars.R",sep = ""))
      source(paste("config_code/",country,"_predictor_vars.R",sep = ""))
      
      for (rhs.group in names(get(table))) {
        
        rhs <- get(table)[[rhs.group]]
        for (algo in c("lasso",
                       "gbm",
                       "rf",
                       "nn",
                       "ebma"
        )) {
          print(paste(v,rhs.group,algo,sep="---"))
          source(paste("estimation_code/appendix/count/conflict_",algo,"_count.R",sep = ""))
        } 
      }
    }
  }
}

for (table in c('tableA4')) {
  for (country in countries) { # {indo, colo}
    for (v in c('demean')) {
      source(paste("config_code/",country,"_data_setup.R",sep = ""))
      source(paste("config_code/",country,"_dependent_vars.R",sep = ""))
      source(paste("config_code/",country,"_predictor_vars.R",sep = ""))
      tableA4 <- table1
      for (rhs.group in names(get(table))) {
        
        rhs <- get(table)[[rhs.group]]
        for (algo in c("lasso",
                       "gbm",
                       "rf",
                       "nn",
                       "ebma"
        )) {
          print(paste(v,rhs.group,algo,sep="---"))
          source(paste("estimation_code/appendix/demean/conflict_",algo,"_demean.R",sep = ""))
        } 
      }
    }
  }
}

for (table in c('tableA5')) {
  for (country in c('indo')) { # {indo, colo}
    for (v in vars) {
      source(paste("config_code/",country,"_data_setup.R",sep = ""))
      source(paste("config_code/",country,"_dependent_vars.R",sep = ""))
      source(paste("config_code/",country,"_predictor_vars.R",sep = ""))
      
      for (rhs.group in names(get(table))) {
        
        rhs <- get(table)[[rhs.group]]
        for (algo in c("lasso",
                       "gbm",
                       "rf",
                       "nn",
                       "ebma"
        )) {
          print(paste(v,rhs.group,algo,sep="---"))
          source(paste("estimation_code/main/conflict_",algo,".R",sep = ""))
        } 
      }
    }
  }
}

for (table in c('tableA7',
                'figure_A3_fixed',
                'figure_A3_annual',
                'figure_A3_slow')) {
  for (country in countries) { # {indo, colo}
    for (v in vars) {
      source(paste("config_code/",country,"_data_setup.R",sep = ""))
      source(paste("config_code/",country,"_dependent_vars.R",sep = ""))
      source(paste("config_code/",country,"_predictor_vars.R",sep = ""))
      source(paste("config_code/onset_filter.R",sep = ""))
      for (rhs.group in names(get(table))) {
        
        rhs <- get(table)[[rhs.group]]
        for (algo in c("lasso",
                       "gbm",
                       "rf",
                       "nn",
                       "ebma"
        )) {
          print(paste(v,rhs.group,algo,sep="---"))
          source(paste("estimation_code/main/conflict_",algo,".R",sep = ""))
        } 
      }
    }
  }
}

for (table in c('tableA8', 'figure_A4_annual',
                        'figure_A4_fixed', 'figure_A4_slow')) {
  for (country in countries) { # {indo, colo}
    for (v in c('spikeHalf', 'spike2YHalf', 'spike2Y')) {
      source(paste("config_code/",country,"_data_setup.R",sep = ""))
      source(paste("config_code/",country,"_dependent_vars.R",sep = ""))
      source(paste("config_code/",country,"_predictor_vars.R",sep = ""))
      tableA8 = table1
      figure_A4_annual = figure_1_annual
      figure_A4_fixed = figure_1_fixed
      figure_A4_slow = figure_1_slow
      for (rhs.group in names(get(table))) {
        
        rhs <- get(table)[[rhs.group]]
        for (algo in c("lasso",
                       "gbm",
                       "rf",
                       "nn",
                       "ebma"
        )) {
          print(paste(v,rhs.group,algo,sep="---"))
          source(paste("estimation_code/main/conflict_",algo,".R",sep = ""))
        } 
      }
    }
  }
}

for (table in c('figure_A5')) {
  for (country in countries) { # {indo, colo}
    for (v in vars) {
      source(paste("config_code/",country,"_data_setup.R",sep = ""))
      source(paste("config_code/",country,"_dependent_vars.R",sep = ""))
      source(paste("config_code/",country,"_predictor_vars.R",sep = ""))
      for (rhs.group in names(get(table))[1]) {
        
        rhs <- get(table)[[rhs.group]]
        for (algo in c("lasso",
                       "gbm",
                       "rf",
                       "nn",
                       "ebma"
        )) {
          print(paste(v,rhs.group,algo,sep="---"))
          source(paste("estimation_code/main/conflict_",algo,".R",sep = ""))
        } 
      }
    }
  }
}


for (table in c('tableA9', 'figure_A8_annual', 'figure_A8_fixed')) {
  for (v in vars) {
    source(paste("config_code/",country,"_data_setup.R",sep = ""))
    source(paste("config_code/colo_ext_dependent_vars.R",sep = ""))
    source(paste("config_code/colo_ext_predictor_vars.R",sep = ""))
    tableA9 <- table1
    figure_A5_annual <- figure_1_annual
    figure_A5_fixed <- figure_1_fixed
    
    for (rhs.group in names(get(table))) {
      
      rhs <- get(table)[[rhs.group]]
      for (algo in c("lasso",
                     "gbm",
                     "rf",
                     "nn",
                     "ebma"
      )) {
        print(paste(v,rhs.group,algo,sep="---"))
        source(paste("estimation_code/main/conflict_",algo,".R",sep = ""))
      } 
    }
  }
}

for (table in c('figureA6')) {
  print('No models need to be run ahead of the figure code.')
}


for (table in c('figureA7')) {
  print('No models need to be run ahead of the figure code.')
}

for (table in c('tableB1')) {
  for (country in countries) { # {indo, colo}
    for (v in vars) {
      source(paste("config_code/",country,"_data_setup.R",sep = ""))
      source(paste("config_code/",country,"_dependent_vars.R",sep = ""))
      source(paste("config_code/",country,"_predictor_vars.R",sep = ""))
      cv.runs <- 1
      fileext <- "_1cv"
      for (rhs.group in names(get("table1"))) {
        
        rhs <- get("table1")[[rhs.group]]
        for (algo in c("lasso",
                       "gbm",
                       "rf",
                       "nn",
                       "ebma"
        )) {
          print(paste(v,rhs.group,algo,sep="---"))
          source(paste("estimation_code/main/conflict_",algo,".R",sep = ""))
        } 
      }
    }
  }
}


for (table in c('tableB2')) {
  for (country in c('indo', 'colo')) { # {indo, colo}
    for (v in c('any','high','spike')) {
      source(paste("config_code/",country,"_data_setup.R",sep = ""))
      source(paste("config_code/",country,"_dependent_vars.R",sep = ""))
      source(paste("config_code/",country,"_predictor_vars.R",sep = ""))
      for (rhs.group in names(get("table1"))) {
        
        rhs <- get("table1")[[rhs.group]]
        for (algo in c("nn2",
          "nn3",
          "nn4",
          "nn5"
        )) {
          print(paste(v,rhs.group,algo,sep="---"))
          source(paste("estimation_code/appendix/nn_alternatives/conflict_",algo,".R",sep = ""))
        } 
      }
    }
  }
}

for (table in c('AppendixC')) {
  print('placeholder')
}

# Make figures
source('exhibit_code/make_appendix_exhibits.R')